package com.rms.utils.service;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.rms.dao.AdminDao;
import com.rms.dao.CompanyDao;
import com.rms.dao.CompanyManagerDao;
import com.rms.dao.HrDao;
import com.rms.dao.JobDao;
import com.rms.dao.ResumeDao;
import com.rms.dao.ResumeJobDao;
import com.rms.dao.WorkerDao;
import com.rms.domain.Admin;
import com.rms.domain.CompanyManager;
import com.rms.domain.Hr;
import com.rms.domain.LoginForm;
import com.rms.domain.Worker;


/**BaseService内置各种Dao类
 * @author Zheng weiyu
 * @date 2019年7月30日
 */
public class BaseService {

	private InputStream in;
	private SqlSession sqlSession;

	protected WorkerDao workerDao;
	protected HrDao hrDao;
	protected AdminDao adminDao;
	protected CompanyManagerDao companyManagerDao;
	protected ResumeDao resumeDao;
	protected ResumeJobDao resumeJobDao;
	protected JobDao jobDao;
	protected CompanyDao companyDao;

	public BaseService() {
		try {
			init();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void init() throws IOException {
		in = Resources.getResourceAsStream("SqlMapConfig.xml");
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
		SqlSessionFactory sqlSessionFactory = builder.build(in);
		sqlSession = sqlSessionFactory.openSession(true);
		adminDao = sqlSession.getMapper(AdminDao.class);
		// companyDao = sqlSession.getMapper(CompanyDao.class);
		companyManagerDao = sqlSession.getMapper(CompanyManagerDao.class);
		hrDao = sqlSession.getMapper(HrDao.class);
		jobDao = sqlSession.getMapper(JobDao.class);
		resumeDao = sqlSession.getMapper(ResumeDao.class);
		resumeJobDao = sqlSession.getMapper(ResumeJobDao.class);
		// roleDao = sqlSession.getMapper(RoleDao.class);
		workerDao = sqlSession.getMapper(WorkerDao.class);
		companyDao = sqlSession.getMapper(CompanyDao.class);
	}

	public void destory() throws IOException {
		sqlSession.close();
		in.close();
	}

	
	/**登录功能，传入需要登录的对象以及其class
	 * @param user
	 * @param loginClass
	 * @return
	 * @throws Exception
	 * @author Zheng weiyu
	 * 2019年7月30日
	 */
	public Object login(LoginForm loginForm, Class<?> loginClass) throws Exception {
		int index = -1;//索引，默认指向-1
		if (loginClass == Admin.class) {
			//管理员登录
			index = 4;
		} else if (loginClass == CompanyManager.class) {
			//公司管理员登录
			index = 3;
		} else if (loginClass == Hr.class) {
			//HR登录
			index = 2;
		} else if (loginClass == Worker.class) {
			//worker登录
			index = 1;
		}
		//登录校验
		//用户名校验在BaseServlet中实现了
		if (loginForm.getPassword() == null || "".equals(String.valueOf(loginForm.getPassword()))) {
			throw new RuntimeException("密码不允许为空");
		}
		if (String.valueOf(loginForm.getId()).length() != 8) {
			throw new RuntimeException("请输入正确的8位数用户名");
		}
		if (loginForm.getPassword().length() < 6 || loginForm.getPassword().length() > 32) {
			throw new RuntimeException("密码长度为6——32位数");
		}
		Object obj = null;
		boolean flag = false;
		//判断密码是否和表单一致
		switch (index) {
		case 4:
			obj = adminDao.get(loginForm.getId());
			if (((Admin) obj).getPassword().equals(loginForm.getPassword())) {	
				flag = true;
			}
			break;
		case 3:
			obj = companyManagerDao.get(loginForm.getId());
			if (((CompanyManager) obj).getPassword().equals(loginForm.getPassword())) {
				flag = true;
			}
			break;
		case 2:
			obj = hrDao.get(loginForm.getId());
			if (((Hr) obj).getPassword().equals(loginForm.getPassword())) {
				flag = true;
			}
			break;
		case 1:
			obj = workerDao.get(loginForm.getId());
			if (((Worker) obj).getPassword().equals(loginForm.getPassword())) {
				flag = true;
			}
			break;
		default:
			break;
		}
		if(!flag) {
			throw new RuntimeException("密码错误");
		}
		
		return obj;
	}
}
